package com.cq.hd.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.member.api.dto.MyMerchantBusinessPageDto;
import com.cq.hd.member.api.dto.UpdateBusinessMerchantOrderDto;
import com.cq.hd.member.api.vo.*;
import com.cq.hd.member.po.TbBusinessMerchantPo;
import com.cq.hd.member.mapper.TbBusinessMerchantMapper;
import com.cq.hd.member.service.TbBusinessMerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 商家商户关联表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-21
 */
@Service
public class TbBusinessMerchantServiceImpl extends ServiceImpl<TbBusinessMerchantMapper, TbBusinessMerchantPo> implements TbBusinessMerchantService {

    @Override
    public BusinessMerchantVo getOwnByBusinessId(Long businessId, Integer ownState) {
        List<TbBusinessMerchantPo> tbBusinessMerchantPos = baseMapper.selectList(new QueryWrapper<TbBusinessMerchantPo>()
                .eq("business_id", businessId).eq("own_state", ownState));
        if (!CollectionUtils.isEmpty(tbBusinessMerchantPos)) {
            TbBusinessMerchantPo tbBusinessMerchantPo = tbBusinessMerchantPos.get(0);
            BusinessMerchantVo businessMerchantVo = new BusinessMerchantVo();
            BeanUtils.copyProperties(tbBusinessMerchantPo, businessMerchantVo);
            return businessMerchantVo;
        }
        return null;
    }

    @Override
    public Page<MyMerchantBusinessPageVo> pageMyBusiness(MyMerchantBusinessPageDto myMerchantBusinessPageDto) {
        Long merchantId = myMerchantBusinessPageDto.getMerchantId();
        if (merchantId == null || merchantId <= 0) {
            Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
        }

        PageHelper.startPage(myMerchantBusinessPageDto.getPageNum(), myMerchantBusinessPageDto.getPageSize());
        List<MyMerchantBusinessPageVo> businessPageVos = baseMapper.lisMyBusinessByDto(myMerchantBusinessPageDto);
        PageInfo<MyMerchantBusinessPageVo> pageInfo = new PageInfo<>(businessPageVos);
        return Page.newPageWithAllFields(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages());
    }

    @Override
    public List<MyMerchantBusinessPageVo> listMyBusiness(MyMerchantBusinessPageDto myMerchantBusinessPageDto) {
        Long merchantId = myMerchantBusinessPageDto.getMerchantId();
        if (merchantId == null || merchantId <= 0) {
            Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
        }

        return baseMapper.lisMyBusinessByDto(myMerchantBusinessPageDto);
    }

    @Override
    public List<MerchantPageVo> listMerchantByBusinessId(Long businessId) {
        return baseMapper.listMerchantByBusinessId(businessId);
    }

    @Override
    public Boolean updateBusinessMerchantOrder(UpdateBusinessMerchantOrderDto updateBusinessMerchantOrderDto) {
        return baseMapper.updateBusinessMerchantOrder(updateBusinessMerchantOrderDto);
    }

    @Override
    public List<BusinessMerchantVo> listOwnMerchantByBusinessIds(List<Long> businessIds) {
        return baseMapper.listOwnMerchantByBusinessIds(businessIds);
    }

    @Override
    public List<MasterBusinessVo> listMasterBusinessByMerchantIds(List<Long> merchantIds) {
        return baseMapper.listMasterBusinessByMerchantIds(merchantIds);
    }
}
